令人驚訝,我竟然在 UIApplicationDelegate 找到關於 app delegate 的說明:
The app delegate works alongside the app object to ensure your app interacts properly with the system and with other apps. Specifically, the methods of the app delegate give you a chance to respond to important changes. For example, you use the methods of the app delegate to respond to state transitions, such as when your app moves from foreground to background execution, and to respond to incoming notifications. In many cases, the methods of the app delegate are the only way to receive these important notifications.
Xcode provides an app delegate class for every new project, so you do not need to define one yourself initially. When your app launches, UIKit automatically creates an instance of the app delegate class provided by Xcode and uses it to execute the first bits of custom code in your app. All you have to do is take the class that Xcode provides and add your custom code.
The app delegate is effectively the root object of your app. Like the UIApplication object itself, the app delegate is a singleton object and is always present at runtime. Although the UIApplication object does most of the underlying work to manage the app, you decide your app’s overall behavior by providing appropriate implementations of the app delegate’s methods. Although most methods of this protocol are optional, you should implement most or all of them.
The app delegate performs several crucial roles:
- It contains your app’s startup code.
- It responds to key changes in the state of your app. Specifically, it responds to both temporary interruptions and to changes in the execution state of your app, such as when your app transitions from the foreground to the background.
- It responds to notifications originating from outside the app, such as remote notifications (also known as push notifications), low-memory warnings, download completion notifications, and more.
- It determines whether state preservation and restoration should occur and assists in the preservation and restoration process as needed.
- It responds to events that target the app itself and are not specific to your app’s views or view controllers.
- You can use it to store your app’s central data objects or any content that does not have an owning view controller.
說真的還是沒有到非常詳細,可以知道到底 AppDelegate 在整個 App 運作的角色,不過我想是因為大部分都已經自動幫我們處理了,所以蘋果也沒有講太多吧,講來講去就是說我們可以在這裡實作 UIApplicationDelegate 的方法(這我早就知道了啊!!)。